package com.zhijian.medical.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.zhijian.medical.config.YyzxConfigProperties;
import com.zhijian.medical.entity.dto.FaceMeetingSyncMeetingRequestDto;
import com.zhijian.medical.entity.dto.FaceMeetingSyncMeetingResponseDto;
import com.zhijian.medical.entity.dto.FaceMeetingUpdateMeetingRequestDto;
import com.zhijian.util.AssertUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author HCG
 * @version 1.0
 * @date 2024-09-24
 */
@Slf4j
public class FaceMeetingUtil {

    /**
     * 超时时间，单位毫秒，3秒
     */
    static int TIME_OUT = 1000 * 3;

    /**
     * URL前缀
     */
    static String URL_FRONT = "https://test.eayuns.com/%s";

    static String keyid = "RGdLNE8rbG1XMjZoNzZ2TzBoWmh5UT09";

    /**
     * 推送会议
     *
     * @param faceMeetingSyncMeetingRequestDto
     * @return
     */
    public static FaceMeetingSyncMeetingResponseDto setMeeting(FaceMeetingSyncMeetingRequestDto faceMeetingSyncMeetingRequestDto) {
        faceMeetingSyncMeetingRequestDto.setKeyeid(SpringUtil.getBean(YyzxConfigProperties.class).getKeyid());
        //链式构建请求
        try {
            String body = com.zhijian.util.JsonUtil.toString(faceMeetingSyncMeetingRequestDto);
            log.info("setMeeting request:{}", body);
            HttpResponse httpResponse = HttpRequest.post(String.format(SpringUtil.getBean(YyzxConfigProperties.class).getApi(), "mobile/vawebapis/setmeeting"))
                    .header("Content-Type", "application/json")
                    .body(body)
                    //超时，毫秒
                    .timeout(TIME_OUT)
                    .execute();
            String responseBody = httpResponse.body();
            log.info("setMeeting response body is {}", responseBody);
            try {
                return com.zhijian.util.JsonUtil.toBean(responseBody, FaceMeetingSyncMeetingResponseDto.class);
            } catch (Exception e) {
                log.error("the response body of setMeeting is not legal...", e.getMessage());
                e.printStackTrace();
                AssertUtil.notNull(null, "推送会议接口失败");
                return null;
            }
        } catch (Exception e) {
            log.error("the request of setMeeting cause error...", e.getMessage());
            e.printStackTrace();
            AssertUtil.notNull(null, "推送会议接口失败");
            return null;
        }
    }

    /**
     * 修改会议
     *
     * @param faceMeetingSyncMeetingRequestDto
     * @return
     */
    public static FaceMeetingSyncMeetingResponseDto updateMeeting(FaceMeetingUpdateMeetingRequestDto faceMeetingSyncMeetingRequestDto) {
        faceMeetingSyncMeetingRequestDto.setKeyeid(SpringUtil.getBean(YyzxConfigProperties.class).getKeyid());
        //链式构建请求
        try {
            String body = com.zhijian.util.JsonUtil.toString(faceMeetingSyncMeetingRequestDto);
            log.info("updateMeeting request:{}", body);
            HttpResponse httpResponse = HttpRequest.post(String.format(SpringUtil.getBean(YyzxConfigProperties.class).getApi(), "mobile/vawebapis/updatemeeting"))
                    .header("Content-Type", "application/json")
                    .body(body)
                    //超时，毫秒
                    .timeout(TIME_OUT)
                    .execute();
            String responseBody = httpResponse.body();
            log.info("updateMeeting response body is {}", responseBody);
            try {
                return com.zhijian.util.JsonUtil.toBean(responseBody, FaceMeetingSyncMeetingResponseDto.class);
            } catch (Exception e) {
                log.error("the response body of updateMeeting is not legal...", e.getMessage());
                e.printStackTrace();
                AssertUtil.notNull(null, "修改会议接口失败");
                return null;
            }
        } catch (Exception e) {
            log.error("the request of updateMeeting cause error...", e.getMessage());
            e.printStackTrace();
            AssertUtil.notNull(null, "修改会议接口失败");
            return null;
        }
    }
}
